package car;

import JDBC.DBHelper;
import UserService.User;
import car.Buycar;
import car.CarInterface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UsercarImp implements CarInterface {

//    添加商品至用户购物车
    @Override
    public void addcart(String username, String managename, String shopname, int quantity, float price,String url) {

        Connection conn= DBHelper.getConn();
        String sql="INSERT  buycar(shop,price,quantity,user,manage,url) VALUE (?,?,?,?,?,?);";
        PreparedStatement stat=null;
        ResultSet rs=null;
        try{

            stat=conn.prepareStatement(sql);
            stat.setString(1, shopname);
            stat.setFloat(2, price);
            stat.setInt(3,quantity );
            stat.setString(4,username);
            stat.setString(5,managename);
            stat.setString(6,url);
            System.out.println("用户的商品添加成功!");
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBHelper.closeAll(conn,stat,rs);
        }
    }


//判断购物车是否有该商品,有则返回true,没有则返回false
    @Override
    public int findcarquantity(String shop, String username,String manage) {
        Connection conn= DBHelper.getConn();
        //  String sql="select * from User WHERE id='"+id+"';";
        String sql="select * from buycar WHERE shop=? and user=? and manage=?;";
        PreparedStatement stat=null;
        ResultSet rs=null;
        User u=new User();
//        boolean hasData = false;
        int quantity=0;
        try {
            stat=conn.prepareStatement(sql);
            stat.setString(1,shop);
            stat.setString(2,username);
            stat.setString(3,manage);
            rs = stat.executeQuery();
            if (rs.next()){
                quantity = rs.getInt("quantity");
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
            //       PreparedStatement stat = null;
            DBHelper.closeAll(conn,stat,rs);
        }
       return quantity;
    }

    //删除商品的方法
    @Override
    public void deletcarshop(String shop, String user, String manage) {
        Connection conn = DBHelper.getConn();
        String sql = "delete from buycar WHERE shop=?,user=?,manage=?";
        PreparedStatement stat = null;
        ResultSet rs = null;
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1, shop);
            stat.setString(2, user);
            stat.setString(3, manage);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBHelper.closeAll(conn, stat, rs);
        }
    }

    @Override
    public void addupdate(String username, String managename, String shopname, int quantity) {
        Connection conn=DBHelper.getConn();
        //  String sql = "UPDATE User SET name='" + user.getName()+"',password='"+user.getPassword()+"', balance = "+user.getBalance()+" WHERE id='"+user.getID()+"';";
        String sql ="UPDATE buycar SET quantity=? WHERE shop=? and manage=? and user=? ";
        PreparedStatement stat = null;
        ResultSet rs=null;
        try {
            stat=conn.prepareStatement(sql);

            stat.setInt(1,quantity);
            stat.setString(2,shopname);
            stat.setString(3, managename);
            stat.setString(4, username);
            stat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBHelper.closeAll(conn,stat,rs);
        }
    }

    @Override
    public ArrayList<Buycar> findShop(String username) {
        System.out.println("hhh");
        Connection conn = DBHelper.getConn();
        //  String sql="select * from User WHERE id='"+id+"';";
        String sql = "select * from buycar WHERE user=?;";//通过管理员manage进行商品的查询
        PreparedStatement stat = null;
        ResultSet rs = null;
        //Shop s=new Shop();

        ArrayList<Buycar> carList = new ArrayList<>();//创建arrayList集合

        //Manage m=new Manage();
        try {

            stat = conn.prepareStatement(sql);
            stat.setString(1, username);
            rs = stat.executeQuery();//executeQuery该方法用来执行查询语句
            while (rs.next()) {
                  Buycar buycar=new Buycar();
                buycar.setShop(rs.getString("shop"));
                buycar.setPrice(rs.getFloat("price"));
                buycar.setManage(rs.getString("manage"));
                buycar.setUser(rs.getString("user"));
                buycar.setQuantity(rs.getInt("quantity"));
                buycar.setUrl(rs.getString("url"));
                carList.add(buycar);//将这个商品添加到ShopList集合中，方便前端到时候进行打印遍历出来
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //       PreparedStatement stat = null;
            DBHelper.closeAll(conn, stat, rs);
        }
        System.out.println("查询结束");

      return carList;
    }
}
